package com.caiheng.api.service.asset.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caiheng.api.dao.asset.EstateAssetCheckRecordMapper;
import com.caiheng.api.dao.asset.EstateAssetMapper;
import com.caiheng.api.entity.asset.EstateAsset;
import com.caiheng.api.entity.asset.EstateAssetCheckRecord;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.asset.IEstateAssetCheckRecordService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 资产盘点记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-20
 */
@Service
public class EstateAssetCheckRecordServiceImpl extends BaseServiceImpl<EstateAssetCheckRecordMapper, EstateAssetCheckRecord> implements IEstateAssetCheckRecordService {

    @Resource
    private EstateAssetCheckRecordMapper assetCheckRecordMapper;
    @Resource
    private EstateAssetMapper assetMapper;

    @Override
    @Transactional
    public int stockTaking(EstateAsset asset, EstateAssetCheckRecord checkRecord) {
        int ret = 0;
        try{
            asset.setLastCheckState(checkRecord.getCheckState());
            asset.setLastCheckTime(new Date());
            ret = assetMapper.updateById(asset);
            if(ret <= 0){
                throw new TransactionalException("assetMapper updateById error");
            }

            ret = assetCheckRecordMapper.insert(checkRecord);
            if(ret <= 0){
                throw new TransactionalException("assetCheckRecordMapper insert error");
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("stockTaking system error");
        }
        return ret;
    }
}
